package com.comyou.beacondemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.comyou.beacon.BeaconClass.Beacon;
import com.comyou.beacon.BeaconManager;
import com.comyou.beacon.BeaconManager.BeaconListener;
import com.comyou.beacon.BeaconRegion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity implements BeaconListener,
		OnItemClickListener {

	Handler handler = new Handler();

	BeaconManager beaconManager;

	ListView listView;

	ListView listview_region;

	ArrayList<Beacon> beacons;
	Map<String, Beacon> scanedBeacons;

	ArrayList<BeaconRegion> monitorRegions = new ArrayList<BeaconRegion>();

	EditText edit_uuid, edit_major, edit_minor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		edit_uuid = (EditText) findViewById(R.id.edit_uuid);
		edit_major = (EditText) findViewById(R.id.edit_major);
		edit_minor = (EditText) findViewById(R.id.edit_minor);

		beacons = new ArrayList<Beacon>();

		scanedBeacons = new HashMap<String, Beacon>();

		beaconManager = new BeaconManager(this);
		beaconManager.setBeaconListener(this);
		listView = (ListView) findViewById(R.id.listview);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(this);

		listview_region = (ListView) findViewById(R.id.listview_region);
		listview_region.setAdapter(regionadapter);

	}

	public void scanAction(View v) {
		beacons.clear();
		scanedBeacons.clear();
		beaconManager.startScanBeacon();
	}

	public void listenAction(View v) {
		String uuid = edit_uuid.getText().toString();

		String major = edit_major.getText().toString();
		String minor = edit_minor.getText().toString();

		BeaconRegion beaconRegion;

		if (major.equals("")) {
			beaconRegion = new BeaconRegion(uuid);
		} else {

			if (minor.equals("")) {
				beaconRegion = new BeaconRegion(uuid, Integer.valueOf(major));
			} else {
				beaconRegion = new BeaconRegion(uuid, Integer.valueOf(major),
						Integer.valueOf(minor));
			}
		}

		monitorRegions.add(beaconRegion);
		regionadapter.notifyDataSetChanged();

		beaconManager.startMonitoringBeaconsInRegion(beaconRegion);

	}

	@Override
	public void onEnterBeaconRegion(BeaconRegion beaconRegion) {
		Toast.makeText(this, "进入区域：" + beaconRegion.toString(),
				Toast.LENGTH_LONG).show();

		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				regionadapter.notifyDataSetChanged();
			}
		});

	}

	@Override
	public void onExitBeaconRegion(BeaconRegion beaconRegion) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "离开区域：" + beaconRegion.toString(),
				Toast.LENGTH_LONG).show();
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				regionadapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void onScanBeacon(Beacon beacon) {

		String key = beacon.uuid + beacon.major + "-" + beacon.minor;
		if (scanedBeacons.containsKey(key)) {

			Beacon mbeacon = scanedBeacons.get(key);
			mbeacon.rssi = beacon.rssi;

		} else {
			scanedBeacons.put(key, beacon);

			beacons.add(beacon);
		}
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				adapter.notifyDataSetChanged();
			}
		});

	}

	private BaseAdapter adapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder1 holder = null;
			if (convertView == null) {
				holder = new ViewHolder1();
				convertView = LayoutInflater.from(parent.getContext()).inflate(
						R.layout.list_item_beacon, null);

				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				holder.tv_uuid = (TextView) convertView
						.findViewById(R.id.tv_uuid);
				holder.tv_major = (TextView) convertView
						.findViewById(R.id.tv_major);
				holder.tv_minor = (TextView) convertView
						.findViewById(R.id.tv_minor);

				holder.tv_rssi = (TextView) convertView
						.findViewById(R.id.tv_rssi);
				holder.tv_state = (TextView) convertView
						.findViewById(R.id.tv_state);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder1) convertView.getTag();
			}

			Beacon beacon = beacons.get(position);

			holder.tv_name.setText(beacon.device.getName());
			holder.tv_uuid.setText(beacon.uuid);
			holder.tv_major.setText("" + beacon.major);
			holder.tv_minor.setText("" + beacon.minor);
			holder.tv_rssi.setText("" + beacon.rssi);

			return convertView;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return beacons.size();
		}

	};

	class ViewHolder1 {
		TextView tv_name;
		TextView tv_uuid;
		TextView tv_major;
		TextView tv_minor;

		TextView tv_rssi;
		TextView tv_state;

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {

	}

	private BaseAdapter regionadapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder2 holder = null;
			if (convertView == null) {
				holder = new ViewHolder2();
				convertView = LayoutInflater.from(parent.getContext()).inflate(
						R.layout.list_item_region, null);

				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				holder.tv_uuid = (TextView) convertView
						.findViewById(R.id.tv_uuid);
				holder.tv_major = (TextView) convertView
						.findViewById(R.id.tv_major);
				holder.tv_minor = (TextView) convertView
						.findViewById(R.id.tv_minor);

				holder.tv_state = (TextView) convertView
						.findViewById(R.id.tv_state);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder2) convertView.getTag();
			}

			BeaconRegion beaconRegion = monitorRegions.get(position);
			holder.tv_uuid.setText(beaconRegion.uuid);

			if (beaconRegion.major == -1) {
				holder.tv_major.setText("");
			} else {
				holder.tv_major.setText("" + beaconRegion.major);
			}
			if (beaconRegion.minor == -1) {
				holder.tv_minor.setText("");
			} else {
				holder.tv_minor.setText("" + beaconRegion.minor);
			}

			if (beaconRegion.hasEntered) {
				holder.tv_state.setText("已进入");
			} else {
				holder.tv_state.setText("已离开");
			}

			return convertView;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return monitorRegions.size();
		}

	};

	class ViewHolder2 {
		TextView tv_name;
		TextView tv_uuid;
		TextView tv_major;
		TextView tv_minor;

		TextView tv_rssi;
		TextView tv_state;

	}
}
